package com.liuyjy.base.common.entity;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author liuyjy
 * @Date 2022/2/7 11:31
 * @Description: 分页返回体
 **/
//@ApiModel(description = "分页返回数据封装对象", value = "分页返回数据封装对象")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class PageResult<T> implements Serializable {

    /**
     * 是否存在上一页
     *
     */
    @ApiModelProperty(value = "是否存在上一页")
    public boolean hasPrevious;
    /**
     * 是否存在下一页
     *
     */
    @ApiModelProperty(value = "是否存在下一页")
    public boolean hasNext;
    //@NotNull(message = "page is null ！")
    @ApiModelProperty(value = "当前页数")
    protected Long page;
    //@NotNull(message = "limit is null ！")
    @ApiModelProperty(value = "查询条数")
    protected Long limit;
    /**
     * 总记录数
     */
    @ApiModelProperty(value = "总记录数")
    private Long total = 0L;
    /**
     * 每页的数据
     */
    @ApiModelProperty(value = "每页的数据")
    private List<T> rows = new ArrayList<>();

    public static <T, E> PageResult<T> getPageResult(Page<E> page, List<T> rows) {
        PageResult<T> pageResult = new PageResult<>();
        pageResult.setRows(rows);
        pageResult.setHasNext(page.hasNext());
        pageResult.setHasPrevious(page.hasPrevious());
        pageResult.setTotal(page.getTotal());
        pageResult.setPage(page.getCurrent());
        pageResult.setLimit(page.getSize());
        return pageResult;
    }
}
